// HOMELESSNESS

		* v1: 12m p1_b only, dropped forever
		* v2 4w  p1_e3wks and
		* v3 2w 5m 7m 12m 2w 2w2w 13m p3_e2w p3_e5w

		* Endline 4w (p1_e3wks) 2w (5m 7m 12m 2w 2w2w 13m 2w 5w)
			foreach x in sleepstreet4w sleepstreet2w {
				gen `x'dys_e = `x'amt_e if `x'amtunit_e ==1
				replace `x'dys_e = `x'amt_e*7 if `x'amtunit_e ==2
				replace `x'dys_e = 0 if  `x'dys_e ==. & `x'_e ==0
				move `x'dys_e `x'amt_e
			}

		* Baseline 4w: p3_b p2_b
			foreach x in sleepstreet4w {
				gen `x'dys_b = `x'amt_b if `x'amtunit_b ==1
				replace `x'dys_b = `x'amt_b*7 if `x'amtunit_b ==2
				replace `x'dys_b = 0 if  `x'dys_b ==. & `x'_b ==0
				move `x'dys_b `x'amt_b
			}

		* Baseline 12m: p1_b
			foreach x in sleepstreet12m {
				gen `x'dys_b = `x'amt_b if `x'amtunit_b ==1
				replace `x'dys_b = `x'amt_b*7 if `x'amtunit_b ==2
				replace `x'dys_b = `x'amt_b*12 if `x'amtunit_b ==3
				replace `x'dys_b = `x'amt_b*365 if `x'amtunit_b ==4
				replace `x'dys_b = 0 if  `x'dys_b ==. & `x'_b ==0
				move `x'dys_b `x'amt_b
			}

		* Replace sleepstreetnow_e with 0 when missing because of the skip
			replace sleepstreetnow_e= 0 if sleepstreet2w_e ==0 & sleepstreetnow_e==.
			replace sleepstreetnow_e= 0 if sleepstreet4w_e ==0 & sleepstreetnow_e==.

			replace sleepstreetnow_b= 0 if sleepstreet12m_b ==0 & p1==1

		* Visually check that everything looks fine
			sum sleepstreetnow_e sleepstreet4w_e sleepstreet4wdys_e if p1_e3wks ==1
			sum sleepstreetnow_e sleepstreet2w_e sleepstreet2wdys_e if p1_e5m ==1
			sum sleepstreetnow_e sleepstreet2w_e sleepstreet2wdys_e if p1_e7m ==1
			sum sleepstreetnow_e sleepstreet2w_e sleepstreet2wdys_e if p1_e12m ==1
			sum sleepstreetnow_e sleepstreet2w_e sleepstreet2wdys_e if p1_e13m ==1
			sum sleepstreetnow_e sleepstreet2w_e sleepstreet2wdys_e if p2_e2wks ==1
			sum sleepstreetnow_e sleepstreet2w_e sleepstreet2wdys_e if p2_e2w2w ==1
			sum sleepstreetnow_e sleepstreet2w_e sleepstreet2wdys_e if p2_e12m ==1
			sum sleepstreetnow_e sleepstreet2w_e sleepstreet2wdys_e if p3_e2w ==1
			sum sleepstreetnow_e sleepstreet2w_e sleepstreet2wdys_e if p3_e5w ==1

			sum sleepstreetnow_b  sleepstreet12m_b sleepstreet12mdys_b if  surveyed_p1_r1_b ==1
			sum sleepstreetnow_b sleepstreet4w_b sleepstreet4wdys_b if  surveyed_p2_r1_b ==1
			sum sleepstreetnow_b sleepstreet4w_b sleepstreet4wdys_b if  p3 ==1

			replace sleepstreet4wdys_e=sleepstreet4wamt_e if sleepstreet4wdys_e>28
			replace sleepstreet4wdys_b=sleepstreet4wamt_b if sleepstreet4wdys_b>28
			replace sleepstreet4wdys_b=28 if sleepstreet4wdys_b>28
			replace sleepstreet2wdys_e=sleepstreet2wamt_e if sleepstreet2wdys_e>14
			replace sleepstreet2wdys_e=14 if sleepstreet2wdys_e>14
			replace sleepstreet12mdys_b=sleepstreet12mamt_b if sleepstreet12mdys_b>365


		* Generate Homeless Dummy
			gen homeless_e = sleepstreetnow_e
				la var homeless_e "Is sleeping on the streets now"

			gen  homeless_b  = sleepstreetnow_b
				la var homeless_b "Is sleeping on the streets now"

		* Harmonize endline 2w and 4w
			replace sleepstreet2wdys_e  = sleepstreet4wdys_e /2 if p1_e3wks ==1
				la var sleepstreet2wdys_e "Number of days slept on the streets (2w)"



** CRIME VICTIMIZATION **

foreach x in victimhouserob3m victimtheft3m victimhouserob4w victimtheft4w {
	gen `x'_resc_e = 1- `x'_e
}
la var victimhouserob3m_resc_e "Victim of home robbery in past 3 months"
la var victimtheft3m_resc_e "Victim of theft in past 3 months"
la var victimhouserob4w_resc_e "Victim of home robbery in past 4 weeks"
la var victimtheft4w_resc_e "Victim of theft in past 4 weeks"

	foreach x in victimhouserob4w_resc victimtheft4w_resc {
		qui sum `x'_e
		qui gen Z_`x'_std_e = (`x'_e - r(mean))/r(sd)
	}
 	qui egen victimizationindex_e = rowmean(Z_*)
	qui sum victimizationindex_e
	*qui replace victimizationindex_e = (victimizationindex_e-r(mean))/r(sd)
	la var victimizationindex_e "Standardized index of victimization"

	rename (Z_*) (*)

	qui gen victimany_e = (victimhouserob4w_resc_e==1)| (victimtheft4w_resc_e==1) if !missing(victimhouserob4w_resc_e) | !missing(victimtheft4w_resc_e)
	la var victimany_e "Victim of Robbery or Theft"
